﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;
using System.IO;
using System.Windows.Forms;

namespace Terminal.Advanced_AEUC_settings_controls.Scripting
{
    /// <summary>
    /// Send message script action
    /// </summary>
    class ScrConcatString : ScrBasicAction
    {
        /// <summary>
        /// Public constructor for the script action
        /// </summary>
        /// <param name="iDispName">Action display text</param>
        /// <param name="iUsageHelp">Usage help and comments text for the action</param>
        /// <param name="callback">Callback function to be used for sending messages</param>
        public ScrConcatString(string iDispName, string iUsageHelp)
            : base(iDispName, iUsageHelp)
        {
            usageHelp += "Input params: string payload structure reference + field of structure to store string + variable number of strings to concatenate\n";
        }

        /// <summary>
        /// Perform the action using specified parameters.
        /// </summary>
        /// <param name="paramList">List of the input/output parameters for the action</param>
        /// <returns>True if action is succesful</returns>
        public override bool Do(List<object> paramList)
        {
            if (breakPoint)
                WaitUserInput();

            if (paramList.Count > 2 && paramList[0].GetType() == typeof(GenPayloadStruct) && paramList[1].GetType() == typeof(char[]))
            {
                GenPayloadStruct pldStruct = (GenPayloadStruct)paramList[0];
                string field = new string(((char[])(paramList[1])));
                string concat = "";

                for (int i = 2; i < paramList.Count; i++)
                {
                    object obj2Print = (object)(paramList[i]);
                    if (obj2Print.GetType() == typeof(GenMSG))
                        concat += ((GenMSG)obj2Print).msgDisplayName;
                    else if (obj2Print.GetType() == typeof(GenPayloadStruct))
                        concat += ((GenPayloadStruct)obj2Print).ContentsString(true);
                    else if (obj2Print.GetType() == typeof(char[]))
                        concat += new string(((char[])obj2Print));
                    else if (obj2Print.GetType() == typeof(int))
                        concat += ((int)obj2Print).ToString();
                    else
                        concat += obj2Print.ToString();
                }

                pldStruct.setStringItem(pldStruct[field], concat);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Returns a List containing objects of the parameter list required for the Do function.
        /// </summary>
        /// <returns>List of parameter objects</returns>
        public override List<Type> GetParamList()
        {
            List<Type> paramList = new List<Type>();
            paramList.Add(typeof(GenPayloadStruct));
            paramList.Add(typeof(char[]));
            paramList.Add(typeof(ScrVarArgListType));
            paramList.Add(typeof(object));

            return paramList;
        }
    }
}




